//
//  ProfileDetailViewController.m
//  cliqset
//
//  Created by David Hinson on 3/2/09.
//  Copyright 2009 Cliqset, Inc.. All rights reserved.
//

#import "ProfileDetailViewController.h"
#import "cliqsetAppDelegate.h"
#import "Event.h"
#import "Relationship.h"

@implementation ProfileDetailViewController

@synthesize index;
@synthesize profile;
@synthesize header;
@synthesize myName;
@synthesize myStatus;
@synthesize myStamp;
@synthesize hoverView;
@synthesize links;

@synthesize relButton;
@synthesize sendButton;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	
    [super viewDidLoad];
	
	Event * event                = [myApp.toEvents objectAtIndex:self.index];
	NSMutableString* sourceUser  = (NSMutableString*)[@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
	self.title                   = sourceUser;	
	self.profile.backgroundColor = [UIColor clearColor];
	
	self.links                   = [[NSMutableArray alloc] init];
	
	[myApp.fromEvents removeAllObjects];
	[self reloadData];

	[self loadHeader:0.0];

    hoverView.center = [self view].center;
	
	[[self view] addSubview:hoverView];
    [[self view] bringSubviewToFront:hoverView];
	
	[self showHoverView:YES];
	
	[myApp.cliqset loadFromEvents:event.userIdentifier reload:YES];
}

- (void)showHoverView:(BOOL)show
{
	// fade animate the view out of view by affecting its alpha
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.40];
    
	if (show)
	{
		hoverView.alpha = 0.85;
	}
	else
	{
		hoverView.alpha = 0.0;
	}
	
	[UIView commitAnimations];
}

-(float)getStatusHeight {
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];	
	UIFont* teeny_font        = [UIFont systemFontOfSize:14];
	
	CGSize constraintSize;
	constraintSize.width        = 220.0;
	constraintSize.height       = MAXFLOAT;	
	CGSize offsetX1             = [[NSString stringWithFormat:@"\"%@\"", myApp.relationshipStatus.status] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
	return offsetX1.height;
}

// Load table header
-(void)loadHeader:(float)size {

	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];	
	Event * event             = [myApp.toEvents objectAtIndex:self.index];
		
	NSData* imageData;
	UIImage * graphic;
	UIImageView *showImage;
	
	if (size>0.0) {
		self.header    = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, size)] retain];
		}
	else {
		self.header    = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 140.0)] retain];
		}
	UIColor* textColor = [UIColor colorWithRed:62.0/255.0 green:62.0/255.0 blue:62.0/255.0 alpha:1.0];
	
	// Fonts for Labels
	UIFont* eeeny_teeny_font  = [UIFont systemFontOfSize:10];
	UIFont* teeny_font      = [UIFont systemFontOfSize:14];
	UIFont* bold_large_font = [UIFont boldSystemFontOfSize:18];
	
	imageData = event.sourceUser.avatarBlob;
	graphic = (UIImage*)[[UIImage alloc] initWithData:imageData];

	// Allocate image
	showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(230.0, 10.0, 80.0, 80.0 * (graphic.size.height / graphic.size.width))];
	showImage.opaque = NO;    
	showImage.image  = graphic;
	
	UIView * imageBackground  = [[UIView alloc] init];
	CGRect adjFrame           = showImage.frame;
	adjFrame.origin.x--;
	adjFrame.origin.y--;
	adjFrame.size.height+=2;
	adjFrame.size.width+=2;
	imageBackground.backgroundColor = [UIColor grayColor];
	imageBackground.frame     = adjFrame;
	[self.header addSubview:imageBackground];
	[imageBackground release];
	
	// Add picture to the cell
	[self.header addSubview:showImage];
	
	[showImage release];
	[graphic release];
	
	// Allocate Labels
	self.myName = [[[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 190.0, 20.0)] retain];
	
	CGSize constraintSize;
	constraintSize.width      = 250.0f;
	constraintSize.height     = MAXFLOAT;	
	CGSize offsetX            = [@"" sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
	self.myStatus             = [[[UILabel alloc] initWithFrame:CGRectMake(10.0, 35.0, 225.0, offsetX.height)] retain];
	
	self.myStamp  = [[[UILabel alloc] initWithFrame:CGRectMake(10.0, -20, 225.0, 20.0)] retain];
	
	// Set name label
	self.myName.text = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
	self.myName.font = bold_large_font;
	
	// Set Status
	myStatus.text       = @"";
	self.myStatus.font  = teeny_font;
	
	self.myStamp.font = eeeny_teeny_font;
	
	// Set label background color
	self.myName.backgroundColor   = [UIColor clearColor];
	self.myStatus.backgroundColor = [UIColor clearColor];
	self.myStamp.backgroundColor  = [UIColor clearColor];
	
	// Set text color
	self.myName.textColor       = textColor;
	self.myStatus.textColor     = textColor;
	self.myStamp.textColor      = textColor;
	
	self.myStatus.numberOfLines = 0;
	self.myStatus.lineBreakMode = UILineBreakModeWordWrap;
	
	// Add relationship / send message buttons
	self.relButton  = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	self.sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	
	CGRect relRect  = CGRectMake(5, 100, 150, 38);
	CGRect sendRect = CGRectMake(165, 100, 150, 38);
	
	self.relButton.frame  = relRect;
	self.sendButton.frame = sendRect;
	
	relButton.font  = [UIFont boldSystemFontOfSize:13];
	sendButton.font = [UIFont boldSystemFontOfSize:13];
	
	[relButton setTitleColor:[UIColor colorWithRed:62.0/255.0 green:62.0/255.0 blue:62.0/255.0 alpha:1.0] forState:UIControlStateNormal];
	[sendButton setTitleColor:[UIColor colorWithRed:62.0/255.0 green:62.0/255.0 blue:62.0/255.0 alpha:1.0] forState:UIControlStateNormal];
	
	[sendButton setTitle:@"Send a Message" forState:UIControlStateNormal];
	
	// Look for asked for relationships
	int asked = 0;
	for (int x=0; x<[myApp.askedForRelationships count]; x++) {
		Target * target = (Target *)[myApp.askedForRelationships objectAtIndex:x];
		if ([target.userIdentifier isEqualToString:event.userIdentifier]) {
			asked = 1;
			break;
		}
	}
	
	int related = 0;
	for (int x=0; x<[myApp.relationships count]; x++) {
		Relationship * relationship = (Relationship *)[myApp.relationships objectAtIndex:x];
		if ([event.userIdentifier isEqualToString:relationship.targetIdentifier]) {
			related = 1;
			break;
		}
	}
	
	if (asked) {
		relButton.alpha  = 0.0;
	} 
	else {
		if (related) {
			[relButton setTitle:@"Remove Relationship" forState:UIControlStateNormal];
			[relButton addTarget:self action:@selector(removeRelationship:) forControlEvents:UIControlEventTouchUpInside];
		} 
		else {
			[relButton setTitle:@"Add Relationship" forState:UIControlStateNormal];
			[relButton addTarget:self action:@selector(addRelationship:) forControlEvents:UIControlEventTouchUpInside];
		}
	}
	
	[sendButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
	
	// Add buttons
	[header addSubview:relButton];
	[header addSubview:sendButton];
	
	// Add labels to the cell
	[header addSubview:self.myName];
	[header addSubview:self.myStatus];
	[header addSubview:self.myStamp];
	
	self.profile.tableHeaderView = header;
}

// Process menu button clicks
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	Event * event             = [myApp.toEvents objectAtIndex:self.index];
	
	switch (buttonIndex) {
		case 0:			
			[myApp.cliqset deleteRelationship:event.userIdentifier];
			[myApp.cliqset getRelationships];
			[myApp.cliqset getAskedForRelationshipRequests];
			[myApp slideComposeProfileOut];
			break;
		default:
			break;
	}
}

// Add relationship
-(void)addRelationship:(id)id {
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	Event * event             = [myApp.toEvents objectAtIndex:self.index];
	[myApp.cliqset requestRelationship:event.userIdentifier];
	self.relButton.alpha = 0.0;
	[myApp.cliqset getAskedForRelationshipRequests];
	NSString * prompt = [NSString stringWithFormat:@"%@ will need to confirm this request before a relationship is established.", self.title];
	[myApp addTextToLog:prompt withCaption:@"Add Relationship"];
}

// Remove Relationship
-(void)removeRelationship:(id)id {
	
	UIActionSheet* myAction = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
	
	[myAction addButtonWithTitle:@"Remove Relationship"];
	[myAction addButtonWithTitle:@"Cancel"];
	
	myAction.destructiveButtonIndex=0;
	myAction.cancelButtonIndex=1;
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	[myAction showInView:[myApp window]];
	
	[myAction release];
}

#pragma mark ---- Send Message ----

-(void)sendMessage:(id)id {
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];	
	
	UIBarButtonItem *backBar = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
	self.navigationItem.backBarButtonItem = backBar;	
	[backBar release];
	
	[myApp slideComposeProfileIn:self.index];
}

#pragma mark ---- Header handling ----

-(void)setHeaderName:(NSString*)name {
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];	
	
	self.myName.text        = name;
	NSData * imageData      = myApp.currentProfile.basicProfile.avatarBlob;
	UIImage * graphic       = (UIImage*)[[UIImage alloc] initWithData:imageData];
	
	// Allocate image
	UIImageView * showImage = [[UIImageView alloc] initWithFrame:CGRectMake(230.0, 10.0, 80.0, 80.0 * (graphic.size.height / graphic.size.width))];
	showImage.opaque        = NO;    
	showImage.image         = graphic;
	
	UIView * imageBackground  = [[UIView alloc] init];
	CGRect adjFrame           = showImage.frame;
	adjFrame.origin.x--;
	adjFrame.origin.y--;
	adjFrame.size.height+=2;
	adjFrame.size.width+=2;
	imageBackground.backgroundColor = [UIColor grayColor];
	imageBackground.frame     = adjFrame;
	[self.header addSubview:imageBackground];
	[imageBackground release];
	
	// Add picture to the cell
	[self.header addSubview:showImage];
	
	[showImage release];
	[graphic release];
}

-(void)setHeaderStatus:(NSString*)newStatus timestamp:(NSString*)timestamp {
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];	
	UIFont* teeny_font        = [UIFont systemFontOfSize:14];
	
	CGSize constraintSize;
	constraintSize.width        = 215.0;
	constraintSize.height       = MAXFLOAT;	
	CGSize offsetX1             = [[NSString stringWithFormat:@"\"%@\"", newStatus] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
	
	self.myStatus.text          = [NSString stringWithFormat:@"\"%@\"", newStatus];
	
	CGRect newFrame2            = CGRectMake(self.myStatus.frame.origin.x, self.myStatus.frame.origin.y, 215, offsetX1.height);
	self.myStatus.frame         = newFrame2;
	self.myStatus.numberOfLines = 0;
	
	CGRect newFrame1            = CGRectMake(self.myStamp.frame.origin.x, 30 + offsetX1.height, 215, self.myStamp.frame.size.height);
	self.myStamp.frame          = newFrame1;	
	
	NSDate * tsDate   = [myApp dateFromISO8601:timestamp];
	self.myStamp.text = [myApp describeEventTimeStamp:tsDate];
	
	//NSLog(@"%.0f %.0f", self.myStatus.frame.size.height + 85, self.header.frame.size.height);
	if (self.myStatus.frame.size.height + 87 > self.header.frame.size.height) {
		CGRect headerFrame = self.profile.tableHeaderView.frame;
		headerFrame.size.height      = self.myStatus.frame.size.height + 100;
		self.header.frame            = headerFrame;
		self.profile.tableHeaderView = self.header;
		
		CGRect btnFrame;
		
		btnFrame = self.relButton.frame;
		btnFrame.origin.y = 30 + offsetX1.height + self.myStamp.frame.size.height;
		self.relButton.frame = btnFrame;
		
		btnFrame = self.sendButton.frame;
		btnFrame.origin.y = 30 + offsetX1.height + self.myStamp.frame.size.height;
		self.sendButton.frame = btnFrame;
		}
}

-(void)setHeaderStatus:(NSString*)status timeStamp:(NSDate*)timeStamp {
	
	cliqsetAppDelegate* myApp   = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];	
	UIFont* teeny_font          = [UIFont systemFontOfSize:14];
	
	CGSize constraintSize;
	constraintSize.width        = 215;
	constraintSize.height       = MAXFLOAT;	
	CGSize offsetX1             = [[NSString stringWithFormat:@"\"%@\"", status] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
	
	self.myStatus.text          = [NSString stringWithFormat:@"\"%@\"", status];
	
	CGRect newFrame2            = CGRectMake(self.myStatus.frame.origin.x, self.myStatus.frame.origin.y, 215, offsetX1.height);
	self.myStatus.frame         = newFrame2;
	self.myStatus.numberOfLines = 0;
	
	CGRect newFrame1            = CGRectMake(self.myStamp.frame.origin.x, 30 + offsetX1.height, 215, self.myStamp.frame.size.height);
	self.myStamp.frame          = newFrame1;	
	self.myStamp.text           = [myApp describeEventTimeStamp:timeStamp];
	
	if (self.myStatus.frame.size.height + 87 > self.header.frame.size.height) {
		CGRect headerFrame = self.profile.tableHeaderView.frame;
		headerFrame.size.height      = self.myStatus.frame.size.height + 100;
		self.header.frame            = headerFrame;
		self.profile.tableHeaderView = self.header;
		
		CGRect btnFrame;
		
		btnFrame = self.relButton.frame;
		btnFrame.origin.y = 30 + offsetX1.height + self.myStamp.frame.size.height;
		self.relButton.frame = btnFrame;
		
		btnFrame = self.sendButton.frame;
		btnFrame.origin.y = 30 + offsetX1.height + self.myStamp.frame.size.height;
		self.sendButton.frame = btnFrame;
		}
}

#pragma mark ---- table handling ----

-(void)reloadData {
	[self.profile reloadData];
}

// Number of table sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

// Return rows in table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	return [myApp.fromEvents count];
}

// Set row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
	UIFont* teeny_font        = [UIFont systemFontOfSize:14];
	UIFont* bold_large_font   = [UIFont boldSystemFontOfSize:14];
	
	if ([myApp.fromEvents count] == 0) {return 53.0;}
	
	Event * event = [myApp.fromEvents objectAtIndex:indexPath.row];
	
	if ([event.eventType isEqualToString:@"RelationshipEstablished"] ) {
		return 60;
	}
	
	if ([event.eventType isEqualToString:@"ProfileUpdated"]) {
		return 40;
	}
	
	if ([event.eventType isEqualToString:@"StatusUpdated"] || [event.eventType isEqualToString:@"NotePosted"]) {
		CGSize constraintSize;
		constraintSize.width = 220.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [[@"" stringByAppendingFormat:@"\"%@\"", event.body] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		return offset1.height + 50;
	}
	
	if ([event.eventType isEqualToString:@"ImagesPosted"]) {
		CGSize constraintSize;
		constraintSize.width  = 250.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offsetName = [[@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName] sizeWithFont:bold_large_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset1;
		if (offsetName.width<150) {
			offset1 = [@"" sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
			} 
		else {
			offset1 = [@" " sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
			}
		return offset1.height + 90;
		}
	
	if ([event.eventType isEqualToString:@"LinkPosted"]) {
		CGSize constraintSize;
		constraintSize.width = 250.0f;
		constraintSize.height = MAXFLOAT;
		
		NSString* description = @"";
		CGSize offset1 = [event.title sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];		
		
		for (int x=0; x<[event.resources count]; x++) {
			Resource * resource = [event.resources objectAtIndex:x];
			if ([resource.name isEqualToString:@"Description"]) {
				description = resource.value;
			}
		}
		
		CGSize offset3 = [description sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		return offset1.height + offset3.height + 50;
		}
	
    return 63.0;
}

// Draw cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	NSData      *imageData;
	UIImageView *showImage;
	UILabel     *nameLabel;
	UILabel     *andLabel;
	UILabel     *nameLabel2;
	UILabel     *actionLabel;
	
	cliqsetAppDelegate* myApp = (cliqsetAppDelegate*)[[UIApplication sharedApplication] delegate];
    
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
    [cell backgroundView].opaque=YES;
    [cell backgroundView].clearsContextBeforeDrawing;
	
   	[cell setSelectionStyle:UITableViewCellSelectionStyleNone];	
	// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	UIColor* textColor = [UIColor colorWithRed:62.0/255.0 green:62.0/255.0 blue:62.0/255.0 alpha:1.0];	
	//UIColor* myBGColor = [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:1.0];
	
	Event * event = [myApp.fromEvents objectAtIndex:indexPath.row];
	
	// Fonts for labels
	UIFont* eeeny_teeny_font  = [UIFont systemFontOfSize:10];
	UIFont* teeny_font        = [UIFont systemFontOfSize:14];
	UIFont* bold_teeny_font   = [UIFont boldSystemFontOfSize:14];
	UIFont* bold_large_font   = [UIFont boldSystemFontOfSize:14];
	
	NSDate * tsDate           = [myApp dateFromISO8601:event.createDate];
	NSString * timeDesc		  = [NSString stringWithFormat:@"%@ from %@", [myApp describeEventTimeStamp:tsDate], event.sourceService.name];
	
	CGSize constraintSize;
	constraintSize.width      = 250.0f;
	constraintSize.height     = MAXFLOAT;	
	CGSize offsetX            = [timeDesc sizeWithFont:eeeny_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
	
	// Time stamp
	UILabel * timestamp       = [[UILabel alloc] initWithFrame:CGRectMake(30, cell.frame.size.height, offsetX.width, offsetX.height)];
	timestamp.font            = eeeny_teeny_font;
	timestamp.textColor       = [UIColor grayColor];
	timestamp.backgroundColor = [UIColor clearColor];
	timestamp.text            = timeDesc;
	
	// Profile updated
	if ([event.eventType isEqualToString:@"ProfileUpdated"]) {
		UIImage *graphic = [UIImage imageNamed:@"id_card_edit.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4, 250.0, 20.0)];
		
		// Build Name Label
		nameLabel.text = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		
		CGSize constraintSize;
		constraintSize.width = 260.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1                   = [nameLabel.text sizeWithFont:bold_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset2                   = [@" updated their profile." sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(26.0 + offset1.width, 4, offset2.width, 20.0)];
		
		CGRect newFrame = CGRectMake(30, 4 + offset2.height, timestamp.frame.size.width, timestamp.frame.size.height);
		timestamp.frame = newFrame;
		
		nameLabel.font = bold_teeny_font;
		
		//if (event.sourceUser.gender == nil) {
		actionLabel.text  = @" updated their profile.";
		//	} 
		if ([event.sourceUser.gender isEqualToString:@"Male"]) {
			actionLabel.text  = @" updated his profile.";
			} 
		if ([event.sourceUser.gender isEqualToString:@"Female"]) {
		actionLabel.text  = @" updated her profile.";
			}
		actionLabel.font  = teeny_font;
		} 
	
	// Other added
	if ([event.eventType isEqualToString:@"OtherAdded"]) {
		UIImage *graphic = [UIImage imageNamed:@"user1_information.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4, 250.0, 20.0)];
		
		// Build Name Label
		nameLabel.text = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		
		CGSize constraintSize;
		constraintSize.width = 260.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [nameLabel.text sizeWithFont:bold_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset2    = [event.body sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel       = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4 + offset1.height, offset2.width, 20.0)];		
		
		CGRect newFrame = CGRectMake(30, 4 + offset1.height + offset2.height, timestamp.frame.size.width, timestamp.frame.size.height);
		timestamp.frame = newFrame;
		
		nameLabel.font    = bold_teeny_font;
		
		actionLabel.text  = event.body;
		actionLabel.font  = teeny_font;
	} 
	
	// Other Updated
	if ([event.eventType isEqualToString:@"OtherUpdated"]) {
		UIImage *graphic = [UIImage imageNamed:@"user1_information.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4, 250.0, 20.0)];		
		// Build Name Label
		nameLabel.text = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		
		CGSize constraintSize;
		constraintSize.width = 260.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [nameLabel.text sizeWithFont:bold_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset2    = [event.body sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel       = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4 + offset1.height, offset2.width, 20.0)];		
		
		CGRect newFrame = CGRectMake(30, 4 + offset1.height, timestamp.frame.size.width, timestamp.frame.size.height);
		timestamp.frame = newFrame;
		
		nameLabel.font    = bold_teeny_font;		
		actionLabel.text  = event.body;
		actionLabel.font  = teeny_font;
	} 
	
	// Status updated
	if ([event.eventType isEqualToString:@"StatusUpdated"]) {
		imageData         = event.sourceUser.avatarBlob;
		UIImage * graphic = (UIImage*)[[UIImage alloc] initWithData:imageData];
		showImage         = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((54-(40 * graphic.size.height / graphic.size.width))/2.0), 40.0, 40.0 * (graphic.size.height / graphic.size.width))];		
		showImage.image   = graphic;
		[graphic release];
		
		UIView * imageBackground  = [[UIView alloc] init];
		CGRect adjFrame           = showImage.frame;
		adjFrame.origin.x--;
		adjFrame.origin.y--;
		adjFrame.size.height+=2;
		adjFrame.size.width+=2;
		imageBackground.backgroundColor = [UIColor grayColor];
		imageBackground.frame     = adjFrame;
		[cell addSubview:imageBackground];
		[imageBackground release];
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(50.0,  4, 240.0, 20.0)];
		
		CGSize constraintSize;
		constraintSize.width = 220.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [[@"" stringByAppendingFormat:@"\"%@\"", event.body] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 24, offset1.width,  offset1.height)];
		
		CGRect newFrame = CGRectMake(50, 24 + offset1.height, timestamp.frame.size.width, timestamp.frame.size.height);
		timestamp.frame = newFrame;
		
		nameLabel.font = bold_large_font;
		
		// Build Name Label
		nameLabel.text            = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		actionLabel.text          = [@"" stringByAppendingFormat:@"\"%@\"", event.body];
		actionLabel.font          = teeny_font;
		actionLabel.numberOfLines = 0;
		actionLabel.lineBreakMode = UILineBreakModeWordWrap;
	}
	
	// Note Posted
	if ([event.eventType isEqualToString:@"NotePosted"]) {
		UIImage *graphic = [UIImage imageNamed:@"note_pinned.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0,  4,  250.0, 20.0)];
		
		CGSize constraintSize;
		constraintSize.width = 240.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1 = [[@"" stringByAppendingFormat:@"\"%@\"", event.body] sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 24, offset1.width,  offset1.height)];
		
		CGRect newFrame = CGRectMake(30, 24 + offset1.height, timestamp.frame.size.width, timestamp.frame.size.height);
		timestamp.frame = newFrame;
		
		nameLabel.font = bold_large_font;
		
		// Build Name Label
		nameLabel.text            = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		actionLabel.text          = [@"" stringByAppendingFormat:@"\"%@\"", event.body];
		actionLabel.font          = teeny_font;
		actionLabel.numberOfLines = 0;
	}
	
	if ([event.eventType isEqualToString:@"ImagesPosted"]) {
		UIImage *graphic = [UIImage imageNamed:@"photo_scenery.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0,  4,  250.0, 20.0)];
		
		CGSize constraintSize;
		constraintSize.width  = 250.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1        = [@" " sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offsetName     = [[@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName] sizeWithFont:bold_large_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offsetShared   = [@" posted some photo(s)" sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		if (offsetName.width<150) {
			UILabel * sharedLabel = [[UILabel alloc] initWithFrame:CGRectMake(26.0 + offsetName.width,  5,  offsetShared.width, offsetShared.height)];
			sharedLabel.font      = teeny_font;
			sharedLabel.textColor = textColor;
			sharedLabel.text      = @" posted some photo(s)";
			[cell addSubview:sharedLabel];
			[sharedLabel release];
			} 
		else {
			UILabel * sharedLabel = [[UILabel alloc] initWithFrame:CGRectMake(26.0 + offsetName.width,  5,  offsetShared.width, offsetShared.height)];
			sharedLabel.font      = teeny_font;
			sharedLabel.textColor = textColor;
			sharedLabel.text      = @" posted some";
			[cell addSubview:sharedLabel];
			[sharedLabel release];
			sharedLabel = [[UILabel alloc] initWithFrame:CGRectMake(26.0,  5 + offsetShared.height,  offsetShared.width, offsetShared.height)];
			sharedLabel.font      = teeny_font;
			sharedLabel.textColor = textColor;
			sharedLabel.text      = @"photo(s)";
			[cell addSubview:sharedLabel];
			[sharedLabel release];
			offset1 = [@" " sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
			}
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 24, offset1.width,  offset1.height)];
		
		CGRect newFrame = CGRectMake(26, 70 + offset1.height, timestamp.frame.size.width, timestamp.frame.size.height);
		timestamp.frame = newFrame;
		
		nameLabel.font = bold_large_font;
		
		// Build Name Label
		nameLabel.text            = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		if (event.title!=nil && ![event.title isEqualToString:@""]) {
			//actionLabel.text      = [@"" stringByAppendingFormat:@"\"%@\"", event.title];
			}
		actionLabel.font          = teeny_font;
		actionLabel.numberOfLines = 0;
		
		NSMutableString * link1  = @"";
		NSMutableString * link2  = @"";
		NSMutableString * link3  = @"";
		NSMutableString * link4  = @"";
		NSMutableString * link5  = @"";
		NSMutableData * thumb1   = nil;
		NSMutableData * thumb2   = nil;
		NSMutableData * thumb3   = nil;
		NSMutableData * thumb4   = nil;
		NSMutableData * thumb5   = nil;
		
		for (int x=0; x<[event.resources count]; x++) {
			Resource * resource = [event.resources objectAtIndex:x];
			if ([resource.name isEqualToString:@"Link-1"]) {
				link1 = resource.value;
			}
			if ([resource.name isEqualToString:@"Link-2"]) {
				link2 = resource.value;
			}
			if ([resource.name isEqualToString:@"Link-3"]) {
				link3 = resource.value;
			}
			if ([resource.name isEqualToString:@"Link-4"]) {
				link4 = resource.value;
			}
			if ([resource.name isEqualToString:@"Link-5"]) {
				link5 = resource.value;
			}
			if ([resource.name isEqualToString:@"Thumbnail-1"]) {
				thumb1 = resource.blob;
			}
			if ([resource.name isEqualToString:@"Thumbnail-2"]) {
				thumb2 = resource.blob;
			}
			if ([resource.name isEqualToString:@"Thumbnail-3"]) {
				thumb3 = resource.blob;
			}
			if ([resource.name isEqualToString:@"Thumbnail-4"]) {
				thumb4 = resource.blob;
			}
			if ([resource.name isEqualToString:@"Thumbnail-5"]) {
				thumb5 = resource.blob;
			}
		}
		
		// Setup links
		if (![link1 isEqualToString:@""]) {
			[self.links addObject:link1];
			
			UIButton * linkButton1 = [[UIButton alloc] initWithFrame:CGRectMake(26.0, 24 + offset1.height, 40.0, 40.0)];
			[linkButton1 setBackgroundImage:[UIImage imageWithData:(NSData*)thumb1] forState:UIControlStateNormal];		
			[linkButton1 addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
			linkButton1.tag = [links count];
			[links addObject:link1];
			
			[cell addSubview:linkButton1];
			[linkButton1 release];
		}
		if (![link2 isEqualToString:@""]) {
			[self.links addObject:link2];
			
			UIButton * linkButton2 = [[UIButton alloc] initWithFrame:CGRectMake(76.0, 24 + offset1.height, 40.0, 40.0)];
			[linkButton2 setBackgroundImage:[UIImage imageWithData:thumb2] forState:UIControlStateNormal];		
			[linkButton2 addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
			linkButton2.tag = [links count];
			[links addObject:link2];
			
			[cell addSubview:linkButton2];
			[linkButton2 release];
		}
		
		if (![link3 isEqualToString:@""]) {
			[self.links addObject:link3];
			
			UIButton * linkButton3 = [[UIButton alloc] initWithFrame:CGRectMake(126.0, 24 + offset1.height, 40.0, 40.0)];
			[linkButton3 setBackgroundImage:[UIImage imageWithData:thumb3] forState:UIControlStateNormal];		
			[linkButton3 addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
			linkButton3.tag = [links count];
			[links addObject:link3];
			
			[cell addSubview:linkButton3];
			[linkButton3 release];
		}
		
		if (![link4 isEqualToString:@""]) {
			[self.links addObject:link4];
			
			UIButton * linkButton4 = [[UIButton alloc] initWithFrame:CGRectMake(176.0, 24 + offset1.height, 40.0, 40.0)];
			[linkButton4 setBackgroundImage:[UIImage imageWithData:thumb4] forState:UIControlStateNormal];		
			[linkButton4 addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
			linkButton4.tag = [links count];
			[links addObject:link4];
			
			[cell addSubview:linkButton4];
			[linkButton4 release];
		}
		
		if (![link5 isEqualToString:@""]) {
			[self.links addObject:link5];
			
			UIButton * linkButton5 = [[UIButton alloc] initWithFrame:CGRectMake(226.0, 24 + offset1.height, 40.0, 40.0)];
			[linkButton5 setBackgroundImage:[UIImage imageWithData:thumb5] forState:UIControlStateNormal];		
			[linkButton5 addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
			linkButton5.tag = [links count];
			[links addObject:link5];
			
			[cell addSubview:linkButton5];
			[linkButton5 release];
		}
	}
	
	// Link posted
	if ([event.eventType isEqualToString:@"LinkPosted"]) {
		UIImage *graphic = [UIImage imageNamed:@"link.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5, 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0,  4,  250.0, 20.0)];
		
		CGSize constraintSize;
		constraintSize.width = 250.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1        = [event.title sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offsetName     = [[@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName] sizeWithFont:bold_large_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offsetShared   = [@" shared a link" sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		UILabel * sharedLabel = [[UILabel alloc] initWithFrame:CGRectMake(26.0 + offsetName.width,  5,  offsetShared.width, offsetShared.height)];
		sharedLabel.font      = teeny_font;
		sharedLabel.textColor = textColor;
		sharedLabel.text      = @" shared a link";
		[cell addSubview:sharedLabel];
		[sharedLabel release];
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 24, offset1.width,  offset1.height)];
		
		nameLabel.font = bold_large_font;
		
		// Build Name Label
		nameLabel.text            = [@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		if (event.title!=nil && ![event.title isEqualToString:@""]) {
			actionLabel.text      = [@"" stringByAppendingFormat:@"%@", event.title];
		}
		actionLabel.font          = teeny_font;
		actionLabel.numberOfLines = 0;
		
		NSString* description = @"";
		NSString* link        = @"Unknown";
		
		CGSize offset3;
		
		for (int x=0; x<[event.resources count]; x++) {
			Resource * resource = [event.resources objectAtIndex:x];
			if ([resource.name isEqualToString:@"Description"]) {
				description = resource.value;
			}
			if ([resource.name isEqualToString:@"Link"]) {
				link = resource.value;
			}
		}
		
		offset3                   = [description sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		UILabel * linkLabel       = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 24 + offset1.height, offset3.width, offset3.height)];		
		UIButton * linkButton     = [[UIButton alloc] initWithFrame:actionLabel.frame];
		
		CGRect newFrame = CGRectMake(26, 24 + offset1.height + offset3.height, timestamp.frame.size.width, timestamp.frame.size.height);
		timestamp.frame = newFrame;
		
		[linkButton addTarget:self action:@selector(doLink:) forControlEvents:UIControlEventTouchUpInside];
		linkButton.tag = [links count];
		[links addObject:link];
		
		linkLabel.backgroundColor = [UIColor clearColor];
		linkLabel.textColor       = textColor;
		linkLabel.text            = description;
		linkLabel.font            = teeny_font;
		linkLabel.numberOfLines   = 0;
		
		[cell addSubview:linkButton];
		[linkButton release];
		
		[cell addSubview:linkLabel];
		[linkLabel release];
		}
	
	if ([event.eventType isEqualToString:@"RelationshipEstablished"]) {
		UIImage *graphic = [UIImage imageNamed:@"users3_add.png"];
		showImage        = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, ((30-(16 * graphic.size.height / graphic.size.width))/2.0), 16, 16 * (graphic.size.height / graphic.size.width))];		
		showImage.image  = graphic;
		
		// Allocate labels
		nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(26.0, 4, 250.0, 20.0)];
		
		NSMutableString * name = (NSMutableString*)[@"" stringByAppendingFormat:@"%@ %@", event.sourceUser.givenName, event.sourceUser.familyName];
		
		// Build Name Label
		nameLabel.text = name;
		
		CGSize constraintSize;
		constraintSize.width = 260.0f;
		constraintSize.height = MAXFLOAT;
		
		CGSize offset1    = [name sizeWithFont:bold_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset2    = [@"and " sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		andLabel                 = [[UILabel alloc] initWithFrame:CGRectMake(offset1.width + offset2.width, 4, offset2.width, 20.0)];
		andLabel.text            = @"and ";
		andLabel.font            = teeny_font;
		andLabel.textColor       = textColor;
		andLabel.backgroundColor = [UIColor clearColor];
		
		CGSize offset3           = [andLabel.text sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset4           = [event.body sizeWithFont:bold_teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		CGSize offset5           = [@" established a relationship." sizeWithFont:teeny_font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
		
		nameLabel2                 = [[UILabel alloc] initWithFrame:CGRectMake(offset1.width + offset2.width + offset3.width, 4, offset4.width, 20.0)];
		nameLabel2.font            = bold_teeny_font;
		nameLabel2.textColor       = textColor;
		nameLabel2.backgroundColor = [UIColor clearColor];
		nameLabel2.text            = event.body;
		
		actionLabel    = [[UILabel alloc] initWithFrame:CGRectMake(26, 4 + offset1.height, offset5.width, 20.0)];
		
		CGRect newFrame = CGRectMake(30, 24 + offset1.height, timestamp.frame.size.width, timestamp.frame.size.height);
		timestamp.frame = newFrame;
		
		nameLabel.font = bold_teeny_font;
		
		[cell addSubview:andLabel];
		[cell addSubview:nameLabel2];
		
		[andLabel release];
		[nameLabel2 release];
		
		actionLabel.text  = @" established a relationship.";
		actionLabel.font  = teeny_font;
	}
	
    showImage.opaque          = NO;    
	
	nameLabel.backgroundColor        = [UIColor clearColor];
	actionLabel.backgroundColor      = [UIColor clearColor];
	
	// Set text color
	nameLabel.textColor       = textColor;
	if ([event.eventType isEqualToString:@"LinkPosted"]) {
		actionLabel.textColor     = [UIColor colorWithRed:255.0/255.0 green:155.0/255.0 blue:5.0/255.0 alpha:1.0];
	} 
	else 
	{
		actionLabel.textColor = textColor;
	}
	cell.textColor            = textColor;
	
	// Set label in cell
	[cell addSubview:nameLabel];		
	// Set label in cell
	[cell addSubview:actionLabel];	
	// Add image to cell
	[cell addSubview:showImage];
	
	// Add time stamp
	[cell addSubview:timestamp];
	
	// Cleanup our junk
	[timestamp release];
	[showImage release];
	[nameLabel release];
	[actionLabel release];
	
	// if we are on row zero, we are done and should hide the loading screen
	if (indexPath.row == 0) {
		[self showHoverView:NO];    
	}
	
	return cell;
}	

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (void)dealloc {
	[relButton release];
	[sendButton release];
	[links release];
	[hoverView release];
	[myName release];
	[myStamp release];
	[myStatus release];
	[header release];
	[profile release];
    [super dealloc];
}


@end
